package com.example.administrator.yuejian_2.activity;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.RegBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.RegService;



import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText usertel;
    private EditText username;
    private EditText userpass;
    private EditText email;
    private Button reg;
    private Button relogin;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        initViews();
        initEvents();
    }

    public void initViews() {
        usertel = (EditText) findViewById(R.id.usertel);
        username = (EditText) findViewById(R.id.username);
        userpass = (EditText) findViewById(R.id.userpass);
        email = (EditText) findViewById(R.id.email);
        reg = (Button) findViewById(R.id.reg);
        relogin = (Button) findViewById(R.id.relogin);

    }

    public void initEvents() {


        reg.setOnClickListener(this);
        relogin.setOnClickListener(this);

//
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.reg:
                //处理登录的点击事件
                if ("".equals(usertel.getText().toString()) || "".equals(username.getText().toString())
                        || "".equals(userpass.getText().toString()))
                    Toast.makeText(RegActivity.this, "不能为空", Toast.LENGTH_LONG).show();
                else
                    reg();
                break;
            case R.id.relogin:
                Intent intent = new Intent();
                finish();
                intent.setClass(RegActivity.this, LoginActivity.class);
                startActivity(intent);


        }


    }

    public void reg() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        final RegService regService = retrofit.create(RegService.class);
        final String tel = usertel.getText().toString();
        String name = username.getText().toString();
        String pass = userpass.getText().toString();
        final String uemail = email.getText().toString();
        Call<RegBean> Call = regService.userReg(name, pass, tel,uemail);
        Call.enqueue(new Callback<RegBean>() {
            @Override
            public void onResponse(Call<RegBean> call, Response<RegBean> response) {
                if (response.body().getInfo().equals("0")) {
                    Toast.makeText(RegActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(RegActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                    String u_id = response.body().getInfo();
                    Intent data = new Intent();
                    data.putExtra("tel", tel);
                    setResult(RESULT_OK, data);
                    finish();
                    data.setClass(RegActivity.this, LoginActivity.class);//从当前界面跳转到MianActivity
                    startActivity(data);
                }
            }

            @Override
            public void onFailure(Call<RegBean> call, Throwable t) {

            }
        });


    }



    }






